package com.example.videoweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.videoweb.domain.Comment;
import com.example.videoweb.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    //添加评论
    @RequestMapping(value = "/add",method = RequestMethod.POST)
        public Object addVideo(HttpServletRequest request){

        String consumerId = request.getParameter("consumerId");
        String videoId = request.getParameter("videoId");
        String content = request.getParameter("content");

        JSONObject jsonObject = new JSONObject();


        Comment comment = new Comment();
        comment.setConsumerId(Integer.parseInt(consumerId));
        comment.setVideoId(Integer.parseInt(videoId));
        comment.setContent(content);
        comment.setPraise(0);

        boolean flag = commentService.insert(comment);
        if (flag) {
            jsonObject.put("code",1);
            jsonObject.put("msg","评论成功");
            return jsonObject;
        }
        jsonObject.put("code",0);
        jsonObject.put("msg","评论失败");
        return jsonObject;
    }

    //删除评论
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Object deleteVideo(HttpServletRequest request){
        String id = request.getParameter("id").trim();
        boolean flag = commentService.delete(Integer.parseInt(id));
        return flag;
    }

    //根据id增加点赞数
    @RequestMapping(value = "/praiseUp",method = RequestMethod.GET)
    public Object praiseUp(HttpServletRequest request){
        String id = request.getParameter("commentId").trim();
        return commentService.praiseUp(Integer.parseInt(id));
    }

    //根据id减少点赞数
    @RequestMapping(value = "/praiseDown",method = RequestMethod.GET)
    public Object praiseDown(HttpServletRequest request){
        String id = request.getParameter("commentId").trim();
        return commentService.praiseDown(Integer.parseInt(id));
    }

    //查询所有前端评论
    @RequestMapping(value = "/allComment",method = RequestMethod.GET)
    public Object allVideo(HttpServletRequest request){
        return commentService.allComment();
    }

    //根据视频id查询评论
    @RequestMapping(value = "/getCommentByVideoId",method = RequestMethod.GET)
    public Object getCommentByVideoId(HttpServletRequest request){
        String videoId = request.getParameter("videoId").trim();
        return commentService.videoComment(Integer.parseInt(videoId));
    }

    //根据id查询评论
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public Object getById(HttpServletRequest request){
        String id = request.getParameter("id").trim();
        return commentService.selectByPrimaryKey(Integer.parseInt(id));
    }

    //根据内容模糊搜索评论
    @RequestMapping(value = "/getByContent",method = RequestMethod.GET)
    public Object getByContent(HttpServletRequest request){
        String content = request.getParameter("content").trim();
        return commentService.getByContent(content);
    }
}


